import axios from 'axios';
import { PaginationData } from '@/types/global';
import { UserVo } from './user';
import { PositionVo } from './position';
import { UserGroupVo } from './user-group';

export interface OrgVo {
  address: string;
  adminer: string;
  authControl: boolean;
  code: string;
  createOrgId: string;
  createOrgName: string;
  createTime: string;
  creator: string;
  creatorName: string;
  deleteFlag: string;
  email: string;
  hasAuthCreateOrgCollection: Array<any>;
  hasAuthCreatorCollection: Array<any>;
  id: string;
  name: string;
  newVersionTime: number;
  operator: string;
  operatorName: string;
  organizationLeader: string;
  organizationTopLeader: string;
  parentId: string;
  phone: string;
  postCode: string;
  simpleName: string;
  sortNumber: number;
  status: string;
  summary: string;
  tenantId: string;
  type: string;
  versionTime: number;
  tenantStatusEnum: string;
}

export interface OrgListParams {
  address?: string;
  adminer?: string;
  code?: string;
  email?: string;
  name?: string;
  organizationLeader?: string;
  organzationTopLeader?: string;
  phone?: string;
  postCode?: string;
  simpleName?: string;
  tenantId?: string;
}

export interface OrgListRes {
  rows: OrgVo[];
  total: number;
}

export interface OrgTreeVo {
  id?: string;
  name?: string;
  isLeaf?: boolean;
  children?: OrgTreeVo[];
}
export interface QueryOrgByKeywordsVO {
  id: string; // 组织id
  name: string; // 组织名称
  parentId: string; // 组织父id
  parentIds: [];
}
export function selectBatch(params: OrgListParams) {
  return axios.post<OrgVo[]>('tenant/org/selectBatch', params);
}
export function selectOrgByName(orgName: string) {
  return axios.get<OrgVo[]>(`tenant/org/selectOrgByName?orgName=${orgName}`);
}
// 根据父级ID查询组织列表
export function selectOrgByParentId(parentId?: string, type?: string) {
  return axios.post<OrgTreeVo[]>(
    `tenant/org/selectOrgByParentId?parentId=${parentId || ''}&type=${type}`
  );
}
// 根据父级ID查询组织列表
export function selectNextOrgByParentId(
  parentId?: string,
  type?: string,
  keywords?: string
) {
  return axios.post<OrgTreeVo[]>(
    `tenant/org/selectNextOrgByParentId?parentId=${parentId || ''}&type=${
      type || ''
    }&keywords=${keywords || ''}`
  );
}
//  全量组织树
export function selectRootTreeOrganization() {
  return axios.post<OrgTreeVo[]>(`tenant/org/selectRootTreeOrganization`);
}
export function selectOrg() {
  return axios.get<OrgVo[]>('tenant/org/selectOrgByName');
}
// 查询组织下一级
export function selectNextLevelOrg(orgId: string) {
  return axios.get<OrgVo[]>(`tenant/org/selectNextLevelOrg?orgId=${orgId}`);
}

export function selectOrgById(id: string) {
  return axios.post<PaginationData<OrgVo>>(`tenant/org/select?id=${id}`);
}

export function createOrg(params: OrgVo) {
  return axios.post<string>('tenant/org/create', params);
}

export function updateOrg(params: OrgVo) {
  return axios.post<string>('tenant/org/edit', params);
}

export function delOrg(id: string) {
  return axios.get<string>(`tenant/org/delete?id=${id}`);
}

export interface UserByOrg extends UserVo {
  isHighlight: boolean;
  name: string;
  currentOrgId: string;
  currentOrgName: string;
}
export interface UserGroupByOrg extends UserGroupVo {
  isHighlight: boolean;
  name: string;
  currentOrgId: string;
  currentOrgName: string;
}
export interface PositionByOrg extends UserGroupVo {
  isHighlight: boolean;
  name: string;
  currentOrgId: string;
  currentOrgName: string;
}
// 用户
export function queryUsersByOrganizationId(
  organizationId: string,
  params: {
    keyWords: string;
  }
) {
  return axios.get<UserVo[]>(
    `tenant/org/queryUsersByOrganizationId/${organizationId}`,
    {
      params,
    }
  );
}

// 用户组
export function queryUserGroupsByOrganizationId(
  organizationId: string,
  params: {
    keyWords: string;
  }
) {
  return axios.get<UserGroupVo[]>(
    `tenant/org/queryUserGroupsByOrganizationId/${organizationId}`,
    {
      params,
    }
  );
}

// 岗位
export function queryPositionsByOrganizationId(
  organizationId: string,
  params: {
    keyWords: string;
  }
) {
  return axios.get<PositionVo[]>(
    `tenant/org/queryPositionsByOrganizationId/${organizationId}`,
    { params }
  );
}
// 根据关键字模糊查询组织列表
export function selectOrgByKeywords(keywords: string) {
  return axios.post<QueryOrgByKeywordsVO[]>(
    `tenant/org/selectOrgByKeywords?keywords=${keywords}`
  );
}

// 查询组织的上级节点信息
export function getParentOrg(keywords: string) {
  return axios.post<QueryOrgByKeywordsVO[]>(
    `tenant/org/getParentOrg?keywords=${keywords}`
  );
}
